package com.lhd.service.impl;
import com.alibaba.excel.EasyExcel;
import com.lhd.entity.po.*;
import com.lhd.entity.query.RecordQuery;
import com.lhd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private RecordService recordService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserSysClassService userSysClassService;

    @Autowired
    private SysClassService sysClassService;

    @Autowired
    private UserCourseService userCourseService;
    /**
     * 根据活动ID查询签到记录并组装导出数据
     *
     * @param aid 活动ID
     * @return ExcelData 列表
     */
    public ExportDataWrapper getExportDataByAid(Integer aid) {
        // 查询所有与该活动ID相关的签到记录
        Activity activity = activityService.getActivityById(aid);
        User teacher = userService.getUserById(activity.getUid());
        Course course = courseService.getCourseById(activity.getCid());
        String Class = sysClassService.getSysClassById(activity.getSid()).getName();
        RecordQuery query = new RecordQuery();
        query.setAid(aid);
        List<Record> records = recordService.findListByQuery(query);
        query.setStatus(String.valueOf(0));
        Long count=recordService.findCountByQuery(query);
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setCourse(course.getName());
        courseInfo.setTeacher(teacher.getUsername());
        courseInfo.setSignedCount(count);
        courseInfo.setCreateTime(activity.getCreateTime());
        courseInfo.setEndTime(activity.getEndTime());
        switch (activity.getType()){
            case "0":
                courseInfo.setSignType("普通签到");
                break;
            case "1":
                courseInfo.setSignType("手势签到");
                break;
            case "2":
                courseInfo.setSignType("位置签到");
                break;
            case "3":
                courseInfo.setSignType("二维码签到");
                break;
            case "4":
                courseInfo.setSignType("签到码签到");
                break;
        }
        List<StudentSignInfo> studentSignInfoList = new ArrayList<>();
        String className = "";
        for (Record record : records) {
            // 根据stuId查询用户信息
            User user = userService.getUserById(record.getStuId());

            if (user != null) {
                // 组装导出数据
                StudentSignInfo studentSignInfo = new StudentSignInfo();
                studentSignInfo.setName(user.getUsername());
                studentSignInfo.setJobCode(user.getJobCode());
                studentSignInfo.setSchool("安徽师范大学");
                studentSignInfo.setInstitute(user.getInstitute());
                studentSignInfo.setMajor(user.getMajor());
                studentSignInfo.setClassName(Class);
                switch (record.getStatus()){
                    case "0":
                        studentSignInfo.setStatus("已签");
                        break;
                    case "1":
                        studentSignInfo.setStatus("缺勤");
                        break;
                    case "2":
                        studentSignInfo.setStatus("事假");
                        break;
                    case "3":
                        studentSignInfo.setStatus("病假");
                        break;
                    case "4":
                        studentSignInfo.setStatus("公假");
                        break;
                    case "5":
                        studentSignInfo.setStatus("迟到");
                        break;
                    case "6":
                        studentSignInfo.setStatus("早退");
                        break;
                    case "7":
                        studentSignInfo.setStatus("待签到");
                        break;
                }
                studentSignInfo.setCreateTime(record.getCreateTime()); // 直接使用 Date 类型
                className=user.getClassName();
                studentSignInfoList.add(studentSignInfo);
            }
        }
        ExportDataWrapper wrapper = new ExportDataWrapper();
        courseInfo.setClassName(className);
        wrapper.setCourseInfo(courseInfo);
        wrapper.setStudentList(studentSignInfoList);
        return wrapper;
    }
    @Override
    public Long importUsersFromExcel(MultipartFile file,Integer id) throws IOException {
        // 1. 使用EasyExcel读取数据
        List<User> userList = EasyExcel.read(file.getInputStream())
                .head(User.class)
                .sheet()
                .doReadSync();

        // 2. 调用批量插入方法
        List<User> newUsers = new ArrayList<>();
        for (User user : userList) {
            User existingUser = userService.findUserByPhone(user.getPhone());
            if (existingUser == null) {
                newUsers.add(user);
            }
        }
        userService.addBatch(newUsers);
        SysClass sysClass = sysClassService.getSysClassById(id);
        Integer cid = sysClass.getCid();
        List<UserSysClass> userSysClassList = new ArrayList<>();
        List<UserCourse> userCourseList = new ArrayList<>();
        for (User user : userList) {
            int uid = userService.findUserByPhone(user.getPhone()).getId();
            UserSysClass userSysClass = new UserSysClass();
            userSysClass.setUid(uid);
            userSysClass.setSid(id);
            userSysClassList.add(userSysClass);
            UserCourse userCourse = new UserCourse();
            userCourse.setUid(uid);
            userCourse.setCid(cid);
            userCourseList.add(userCourse);
        }
        Long result = userSysClassService.addBatch(userSysClassList);
        sysClass.setTotals(sysClass.getTotals() + userSysClassList.size());
        userCourseService.addBatch(userCourseList);
        sysClassService.updateSysClassById(sysClass,id);
        return result;
    }
}